package org.smartjava.core.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        Thread thread1 = new Thread(() -> {
            try {
                TimeUnit.MICROSECONDS.sleep(500);
                System.out.println("1 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "1");

        Thread thread2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("2 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "2");

        Thread thread3 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("3 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "3");
        thread1.start();
        thread2.start();
        thread3.start();
        countDownLatch.await();

        System.out.println("finished");
    }

    public void countDownLatchTest() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        Thread thread1 = new Thread(() -> {
            try {
                TimeUnit.MICROSECONDS.sleep(500);
                System.out.println("1 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "1");

        Thread thread2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("2 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "2");

        Thread thread3 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("3 goes");
                countDownLatch.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "3");
        thread1.start();
        thread2.start();
        thread3.start();
        countDownLatch.await();

        System.out.println("finished");
    }


    public void processOrder() throws InterruptedException {
        int orderSize = 5;
        CountDownLatch countDownLatch = new CountDownLatch(orderSize);
        for (int i = 0; i < orderSize; i++) {
            new Thread(() -> {
                try {
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "订单订单处理");
            }, "order-processor-" + i).start();
        }
        countDownLatch.await();
        System.out.println("订单都处理完了");

    }
}
